//
//  AKASegmentedControl+FAN.m
//  Fantasia
//
//  Created by kongkong on 16/10/10.
//  Copyright © 2016年 Kongkong. All rights reserved.
//

#import "AKASegmentedControl+FAN.h"
#import <objc/runtime.h>

@implementation AKASegmentedControl (FAN)

-(SegmentSelect)selectedAction{
    return [self associatedValueForKey:"selectedAction"];
}

- (void)setSelectedAction:(SegmentSelect)selectedAction{
    if (!selectedAction) {
        return;
    }
    [self associateValue:selectedAction withKey:"selectedAction"];
}

+ (AKASegmentedControl*)segmentWithFrame:(CGRect)frame images:(NSArray*)images titles:(NSArray*)titles{
    
    AKASegmentedControl* segment = [[AKASegmentedControl alloc]initWithFrame:frame];
        
        // Setting the resizable background image
    UIImage *backgroundImage = [[UIImage imageNamed:@"segmented-bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)];
    [segment setBackgroundImage:backgroundImage];
    
    // Setting the content edge insets to adapte to you design
    [segment setContentEdgeInsets:UIEdgeInsetsMake(2.0, 2.0, 3.0, 2.0)];
    
    // Setting the behavior mode of the control
    [segment setSegmentedControlMode:AKASegmentedControlModeSticky];
    
    // Setting the separator image
    [segment setSeparatorImage:[UIImage imageNamed:@"segmented-separator.png"]];
    
    UIImage *buttonBackgroundImagePressedLeft = [[UIImage imageNamed:@"segmented-bg-pressed-left.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 4.0, 0.0, 1.0)];
            UIImage *buttonBackgroundImagePressedCenter = [[UIImage imageNamed:@"segmented-bg-pressed-center.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 4.0, 0.0, 1.0)];
    UIImage *buttonBackgroundImagePressedRight = [[UIImage imageNamed:@"segmented-bg-pressed-right.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 1.0, 0.0, 4.0)];
    
    NSMutableArray* btns = [NSMutableArray array];
    
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        NSString* imageName = nil;
        if (images.count > idx) {
            imageName = images[idx];
        }

        UIButton *button = [[UIButton alloc] init];
        UIImage *buttonImageNormal = [UIImage imageNamed:imageName];

        UIImage* bgImage =buttonBackgroundImagePressedCenter;
        if (0 == idx) {
            bgImage = buttonBackgroundImagePressedLeft;
        }
        else if (idx == titles.count -1){
            bgImage = buttonBackgroundImagePressedRight;
        }
        
        [button setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 5.0)];
        [button setBackgroundImage:bgImage forState:UIControlStateHighlighted];
        [button setBackgroundImage:bgImage forState:UIControlStateSelected];
        [button setBackgroundImage:bgImage forState:(UIControlStateHighlighted|UIControlStateSelected)];
        [button setImage:buttonImageNormal forState:UIControlStateNormal];
        [button setImage:buttonImageNormal forState:UIControlStateSelected];
        [button setImage:buttonImageNormal forState:UIControlStateHighlighted];
        [button setImage:buttonImageNormal forState:(UIControlStateHighlighted|UIControlStateSelected)];

        [button setTitle:obj forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:81/255.0 green:113/255.0 blue:132/255.0 alpha:1] forState:UIControlStateNormal];

        button.tag = idx + 1001;
        [btns addObject:button];
    }];
    
    [segment setButtonsArray:btns];
    [segment setSelectedIndex:0];
    [segment addTarget:segment action:@selector(clickAction) forControlEvents:UIControlEventValueChanged];
    
    return segment;
}

- (void)clickAction{
//    self.selectedIndexes;
//    NSLog(@"$@",self.selectedIndexes);
    if (self.selectedIndexes > 0 &&
        self.selectedAction) {
        self.selectedAction(self.selectedIndexes.firstIndex);
    }
}

@end
